home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / batchut / bathints.zip / BH_10.DOC < prev    next >
Text File  |  1988-10-16  |  14KB  |  363 lines

  1.  
  2.                                   BAT-HINT # 10
  3.  
  4. **************************************************************************
  5.  
  6. from the BATHINTS library... part of the BATPOWER CONFERENCE from:
  7.  
  8.                          THE PAINFRAME OPUS/FIDO 261/1004 
  9.  
  10.                         Baltimore, Maryland 1-301-488-7461
  11.  
  12. **************************************************************************
  13.  
  14.                                The IF subcommand
  15.  
  16. The IF subcommand is not unique to batch file processing in DOS... the
  17. command may be issued from either a batch file or from the command line,
  18. though it finds its greatest use from within the batch file.  Through the
  19. use of IF a user may check for several things:
  20.  
  21.     1.  whether a file exists
  22.     2.  whether a specific errorlevel has been returned from the keyboard
  23.         or from a program
  24.     3.  whether a variable exists
  25.     4.  the value of a variable or string
  26.  
  27. The general syntax of the IF command is:
  28.  
  29.     if [not] {true statement} {perform command}
  30.  
  31. where [not] is optional, {true statement} is a legal statement to test with
  32. the IF command and {perform command} is any executable command.
  33.  
  34. Errorlevel Testing
  35. ------------------
  36.  
  37. Many of you have no doubt already used the IF command to test for
  38. errorlevels, so lets start with a simple example of an errorlevel test:
  39.  
  40.     sample1.bat
  41.     -----------
  42.  
  43.     echo off
  44.     cls
  45.     :start
  46.     echo Press a key... (you must press the right one!)
  47.     key
  48.     if errorlevel 65 goto correct
  49.     echo Sorry, that was the wrong key...
  50.     goto start
  51.     :correct
  52.     echo You pressed uppercase A
  53.     echo That was the letter I was waiting for!
  54.  
  55. This example uses the KEY.COM utility available from BATPOWER.ARC, a
  56. utility that pauses the execution of a batch file and waits for a single
  57. key to be pressed at the keyboard... and then returns an errorlevel code
  58. equal to the scan code of the key pressed (all keys and key combinations
  59. have an associated scan code used by DOS; for details on the specific scan
  60. codes see BATHINT ? ?... bh_?.doc; for details on the use of KEY.COM see
  61. the doc file KEY.DOC packaged with the program in BATPOWER.ARC).  In this
  62. example, the IF command is waiting for the uppercase (capital) A to be
  63. pressed... its scan code is 65.  Lets examine the command in more detail:
  64.  
  65.     if errorlevel 65 goto correct
  66.  
  67. We can divide the command statement into three parts according to the
  68. syntax already given.  First, IF, well thats what we are talking about. 
  69. Second, errorlevel 65, that is the {true statement} that we are testing...
  70. is the errorlevel 65, or more precisely, is the errorlevel equal to or
  71. greater than 65?  Thats the way testing for errorlevels works.  If a
  72. statement is made like:
  73.  
  74.     if errorlevel 65 {perform command}
  75.  
  76. then the IF command tests whether the errorlevel returned is equal to or
  77. greater than the specified number.  In sample1.bat we could narrow down the
  78. specificity of the test with a statement like:
  79.  
  80.     if errorlevel 65 if not errorlevel 66 {perform command}
  81.  
  82. AhAh!  A nested IF command.  Examine closely what this statement says: If
  83. the errorlevel is 65 or greater AND if the errorlevel is NOT 66 or greater
  84. then {perform command}... so the errorlevel must be equal to 65 (or more)
  85. but not equal to 66 (or more)... thus the errorlevel MUST be 65 for the
  86. statement to be true.  If the statement is true (if an A was pressed) then
  87. {perform command}, which in sample1.bat is GOTO CORRECT, is executed.  If
  88. it is false (if the errorlevel is less than 65) then {perform command} is
  89. not executed and the batch file ignores {perform command} and jumps to the
  90. next line ( the line below).  So with "if errorlevel XX if not errorlevel
  91. XX+1" we may single out a specific errorlevel for testing.  Examining the
  92. third part of the statement "goto correct", we see the command to be
  93. executed IF THE STATEMENT IS TRUE.  GOTO is batch file specific command in
  94. DOS which instructs DOS to jump to a specified label.  In our example, the
  95. label is "correct" (though it could be anything, so long as that label
  96. exists).  Notice that further down in the batch file is the following line:
  97.  
  98.     :correct
  99.  
  100. and notice also that when specifying a label with the GOTO command it is
  101. not necessary to include the colon (:) before the label (but the label must
  102. have a colon where it actually appears as a directive in the batch file).  
  103. So if everything is true (you actually did press the letter A), sample1.bat
  104. performs the "GOTO CORRECT" command and searches the batch file for the
  105. label :correct then continues executing each line, one at a time, from the
  106. label :correct.  In our example, two echo commands are executed indicating
  107. that that was the right key to press... and the batch file ends.  If the
  108. statement was FALSE (you did not press A; the errorlevel was less than and
  109. not equal to 65) then the batch file ignores the "goto correct" command and
  110. skips to the next line, which echos that you pressed the wrong key and then
  111. performs the "goto start" command, which jumps back to the :start label and
  112. asks you to press a key again (the letter A!).
  113.  
  114. So great eh?  What can I do with it?  Well there are many applications,
  115. only one of which is a small menu of possible selections.  As a last
  116. example, consider the following scenerio:  You have three programs from
  117. which to choose and you have written a batch file which starts each
  118. application by its name.  The applications are:  Yorgi (a fanatstic game of
  119. fun and skill?!), RipWrite (a great word processor) and Snarf (a utility
  120. program that you cannot live without).  Lets assume that each batch file
  121. (yorgi.bat, ripwrite.bat and snarf.bat) are available through the normal
  122. DOS command line by virtue of the fact that they are in the c:\bat
  123. subdirectory that is named in your path command... so that they be found
  124. when called upon).  Rather than type the name of the batch file which
  125. starts these great programs, you have created a simple menu from which with
  126. a single keystroke you may select any one of them.  That batch file may be
  127. written like this:
  128.  
  129.     menu.bat
  130.     --------
  131.  
  132.     echo off
  133.     cls
  134.     echo Main Menu:
  135.     echo      1.  Yorgi
  136.     echo      2.  RipWrite
  137.     echo      3.  Snarf
  138.     echo Press a number according to your selection...
  139.     key
  140.     if errorlevel 51 if not errorlevel 52 goto select3
  141.     if errorlevel 50 if not errorlevel 51 goto select2
  142.     if errorlevel 49 if not errorlevel 50 goto select1
  143.     goto sorry
  144.     :select1
  145.     yorgi
  146.     :select2
  147.     ripwrite
  148.     :select3
  149.     snarf
  150.     :sorry
  151.     echo You must press a number between 1 and 3
  152.     pause
  153.     goto start
  154.     cls
  155.  
  156. Notice that the IF ERRORLEVEL statements are numbered backwards from the
  157. scan code for number 3 (51) to the scan code for number 1 (49).  Following
  158. the logic given for sample1.bat, you should be able to see that you must
  159. start from the highest number down (if you have questions just ask!!!).
  160.  
  161. Testing for the Presence of a File
  162. ----------------------------------
  163.  
  164. IF can test for the existance of a file.  Examine the following batch
  165. file:
  166.  
  167.     sample2.bat
  168.     -----------
  169.  
  170.     echo off
  171.     cls
  172.     if exist myword.doc echo myword.doc exists!
  173.     cls
  174.  
  175. Simple enough... sample2.bat tests for the existance of a file called
  176. "myword.doc" and if the statement is true (that is, if myword.doc does
  177. exist), it executes the command "echo myword.doc exists!".  If the file is
  178. not found the remainder of the command line is ignored and the batch file
  179. continues processing at the next line, which in this example clears the
  180. screen.  Note that the "if exist" command only recognizes files in the
  181. current directory.  To test for a file located elsewhere you should log
  182. onto the drive and into the subdirectory where the file is presumed to
  183. exist.  For example, if the file was located on drive B in a subirectory
  184. called "document", you would need to change sample2.bat to read:
  185.  
  186.  
  187.     echo off
  188.     cls
  189.     b:
  190.     cd\document
  191.     if exist b:\document\myword.doc echo myword.doc exists!
  192.  
  193. Occasionally, the test for a specific